package com.cleaningcloud.cld.service.impl;

import java.util.List;

import com.cleaningcloud.common.core.constant.UserConstants;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldProjectAccountMapper;
import com.cleaningcloud.cld.api.domain.CldProjectAccount;
import com.cleaningcloud.cld.service.ICldProjectAccountService;

/**
 * 项目账号信息Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2024-10-15
 */
@Service
public class CldProjectAccountServiceImpl implements ICldProjectAccountService 
{
    @Autowired
    private CldProjectAccountMapper cldProjectAccountMapper;

    /**
     * 查询项目账号信息
     * 
     * @param projectAccountId 项目账号信息主键
     * @return 项目账号信息
     */
    @Override
    public CldProjectAccount selectCldProjectAccountByProjectAccountId(Long projectAccountId)
    {
        return cldProjectAccountMapper.selectCldProjectAccountByProjectAccountId(projectAccountId);
    }

    /**
     * 查询项目账号信息列表
     * 
     * @param cldProjectAccount 项目账号信息
     * @return 项目账号信息
     */
    @Override
    public List<CldProjectAccount> selectCldProjectAccountList(CldProjectAccount cldProjectAccount)
    {
        return cldProjectAccountMapper.selectCldProjectAccountList(cldProjectAccount);
    }

    /**
     * 根据项目ID查询项目账号信息列表
     *
     * @param projectId 项目ID
     * @return 项目账号信息
     */
    @Override
    public List<CldProjectAccount> selectCldProjectAccountListByProjectId(Long projectId)
    {
        return cldProjectAccountMapper.selectCldProjectAccountListByProjectId(projectId);
    }

    /**
     * 新增项目账号信息
     * 
     * @param cldProjectAccount 项目账号信息
     * @return 结果
     */
    @Override
    public int insertCldProjectAccount(CldProjectAccount cldProjectAccount)
    {
        cldProjectAccount.setCreateTime(DateUtils.getNowDate());
        cldProjectAccount.setCreateId(SecurityUtils.getUserId());
        cldProjectAccount.setCreateName(SecurityUtils.getUsername());
        cldProjectAccount.setFlag(0L);
        if(StringUtils.isNotEmpty(cldProjectAccount.getPassWord())){
            String password = cldProjectAccount.getPassWord();
            cldProjectAccount.setPassWord(SecurityUtils.encryptPassword(password));
        }
        return cldProjectAccountMapper.insertCldProjectAccount(cldProjectAccount);
    }

    /**
     * 修改项目账号信息
     * 
     * @param cldProjectAccount 项目账号信息
     * @return 结果
     */
    @Override
    public int updateCldProjectAccount(CldProjectAccount cldProjectAccount)
    {
        return cldProjectAccountMapper.updateCldProjectAccount(cldProjectAccount);
    }

    @Override
    public int resetPwd(CldProjectAccount cldProjectAccount) {
        return cldProjectAccountMapper.updateCldProjectAccount(cldProjectAccount);
    }

    /**
     * 批量删除项目账号信息
     * 
     * @param projectAccountIds 需要删除的项目账号信息主键
     * @return 结果
     */
    @Override
    public int deleteCldProjectAccountByProjectAccountIds(Long[] projectAccountIds)
    {
        return cldProjectAccountMapper.deleteCldProjectAccountByProjectAccountIds(projectAccountIds);
    }

    /**
     * 删除项目账号信息信息
     * 
     * @param projectAccountId 项目账号信息主键
     * @return 结果
     */
    @Override
    public int deleteCldProjectAccountByProjectAccountId(Long projectAccountId)
    {
        return cldProjectAccountMapper.deleteCldProjectAccountByProjectAccountId(projectAccountId);
    }

    @Override
    public CldProjectAccount selectAccountByAccountPhone(String accountPhone) {
        return cldProjectAccountMapper.selectAccountByAccountPhone(accountPhone);
    }

    @Override
    public boolean checkUserPhoneUnique(CldProjectAccount cldProjectAccount) {
        Long accountId = StringUtils.isNull(cldProjectAccount.getProjectAccountId()) ? -1L : cldProjectAccount.getProjectAccountId();
        CldProjectAccount info = cldProjectAccountMapper.checkPhoneUnique(cldProjectAccount.getUserPhone());
        if (StringUtils.isNotNull(info) && info.getProjectAccountId().longValue() != accountId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
